package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.IncludeReplyCmtBottomBinding;
import com.jwdf.uda.databinding.IncludeReplyTopBinding;
import com.jwdf.uda.databinding.ItemReplyCommentTxtBinding;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.event.ReplyDetailItemClickEvent;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.ReplyDetailItem;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.UdaUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2016/9/19.
 */
public class ReplyCmtTextViewHolder extends AbsViewHolder<ReplyDetailItem> {
    private ItemReplyCommentTxtBinding mItemBinding;

    public ReplyCmtTextViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);

        convertView.setOnClickListener(this);
        mItemBinding.topLayout.llHeadLayout.setOnClickListener(this);
    }

    static void onBindCommonView(IncludeReplyTopBinding topLayout, IncludeReplyCmtBottomBinding bottomLayout, final ReplyDetailItem item) {
        final PbBean.PbBaseComment comment = item.comment;
        PbBean.PbUserSimple userInfo = comment.getUserInfo();
        topLayout.tvName.setText(userInfo.getNickname());
        topLayout.setIsDesigner(userInfo.getIsDesigner());
        topLayout.tvIntro.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(userInfo.getIntroduction()))?View.GONE:View.VISIBLE);
        topLayout.tvIntro.setText(userInfo.getIntroduction());
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), topLayout.ivHeader);
        if (StringUtils.isNullEmpty(comment.getContent())) {
            topLayout.tvContent.setVisibility(View.GONE);
        } else {
            topLayout.tvContent.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(comment.getAtUserNickname())) {
                topLayout.tvContent.setText(topLayout.ivHeader.getContext().getString(R.string.item_comment_at,
                        comment.getAtUserNickname(), UdaUtils.filterAtUserString(comment.getContent())));
            } else {
                topLayout.tvContent.setText(UdaUtils.filterAtUserString(comment.getContent()));
            }
        }
        bottomLayout.tvDate.setText(DateUtils.getFriendlyTime(comment.getCreateTime()));

        bottomLayout.tvZan.setText("" + item.commentLikeCount);
        bottomLayout.tvZan.setSelected(item.isLikeComment);
        bottomLayout.tvZan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReplyDetailEvent event = new ReplyDetailEvent(item.isLikeComment ? ReplyDetailEvent.EVENT_UNLIKE_COMMENT : ReplyDetailEvent.EVENT_LIKE_COMMENT);
                event.item = item;
                EventBusUtil.post(event);
            }
        });
    }

    @Override
    protected void onBindView(ReplyDetailItem item) {
        PbBean.PbBaseComment comment = item.comment;
        if (comment == null) return;
        onBindCommonView(mItemBinding.topLayout, mItemBinding.bottomLayout, item);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_head_layout) {
            UserInfoActivity.start(v.getContext(), item.comment.getUserInfo().getUserId(), item.comment.getUserInfo().getIsDesigner());
            return;
        }
        EventBusUtil.post(new ReplyDetailItemClickEvent(ReplyDetailItem.convert(item.comment, ReplyDetailItem.TYPE_COMMENT_TXT)));
    }
}
